﻿using CefSharp;
using Chen.CefWebKit.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chen.CefWebKit.Factory
{
    public class ResponseUrlFilterFacotory
    {
        public string Url { get; set; } = "";
        Action<IRequest, IResponse, byte[]> action;
        public ResponseUrlFilterFacotory(string _url,Action<IRequest, IResponse, byte[]> _action)
        {
            Url = _url;
            action = _action;
        }
        /// <summary>
        /// 判断URL是否符合规则 该方法可以被重写
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        public virtual bool IsFilterUrl(IRequest request, IResponse response)
        {
            string[] urls = request.Url.Split('?');
            if (urls.Count() > 0)
            {
                return Url.ToLower() == urls[0].ToLower();
            }
            return false;
        }
        /// <summary>
        /// 具体执行的操作
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public virtual bool DoProcess(IRequest request, IResponse response,byte[] content)
        {
            try
            {
                action.Invoke(request, response, content);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
    public class ResponseUrlFilterFacotorys
    {
        internal Dictionary<UInt64, MemoryStreamResponseFilter> responseDictionary = new Dictionary<UInt64, MemoryStreamResponseFilter>();
        private List<ResponseUrlFilterFacotory> responseFilterFactorys = new List<ResponseUrlFilterFacotory>();
        /// <summary>
        /// 某个URL是否符合筛选条件
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        internal bool IsFilterUrl(IRequest request=null, IResponse response=null)
        {
            return responseFilterFactorys.Any(i => i.IsFilterUrl(request, response));
        }
        /// <summary>
        /// 处理一个内容
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        /// <param name="content"></param>
        internal void ProcessContent(IRequest request, IResponse response)
        {
            var filters = responseFilterFactorys.Where(i => i.IsFilterUrl(request,response)).ToArray();
            
            if (filters.Count() > 0)
            {
                MemoryStreamResponseFilter filter;
                if (responseDictionary.TryGetValue(request.Identifier, out filter))
                {
                    var data = filter.Data;
                    foreach (var item in filters)
                    {
                        item.DoProcess(request, response, data);
                    }
                }
            }
        }
        /// <summary>
        /// 添加一个ResponseUrlFilterFacotry处理工厂
        /// </summary>
        /// <param name="urlFilterFactory"></param>
        public void AddResponseUrlFilterFacotry(ResponseUrlFilterFacotory urlFilterFactory)
        {
            responseFilterFactorys.Add(urlFilterFactory);
        }
        /// <summary>
        /// 添加一个ResponseUrlFilterFacotry处理工厂
        /// </summary>
        /// <param name="url">URL</param>
        /// <param name="_action">处理方法</param>
        public void AddResponseUrlFilterFacotry(string url, Action<IRequest, IResponse, byte[]> _action)
        {
            responseFilterFactorys.Add(new ResponseUrlFilterFacotory(url,_action));
        }
    }
}
